<?php
namespace App\Http\Controllers;

use App\Http\Controllers\WebController;
use App\Http\Controllers\Business\WaterModule;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class WaterController extends WebController
{
    //水表列表
    public function waterList() {
        $result = WaterModule::waterMeterList(1, 100);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        return view('water.water_list', $result['data']);
    }
    //新增水表页面
    public function waterAdd() {
        if($this->request->has('id')) {
            $result = WaterModule::getWaterMeter($this->request->get('id'));
            if($result['status'] == 'failed') {
                return view('errors.404');
            } else {
                return view('water.water_add', $result['data']);
            }
        } else {
            return view('water.water_add');
        }
    }
    //提交水表新增信息
    public function submitWaterAddData() {
        $number = $this->request->get('number');
        $name = $this->request->get('name');
        $caliber = $this->request->get('caliber');
        $accuracyClass = $this->request->get('accuracy_class');
        $radix = $this->request->get('radix');
        $rate = $this->request->get('rate') ;
        $area = $this->request->get('area');
        $latitude = $this->request->get('latitude');
        $longitude = $this->request->get('longitude');
        $enterpriseName = $this->request->get('enterprise_name');
        $lastTime = date('Y-m-d H;i;s');
        $period = 0;
        $fatherNode = $this->request->get('father_node');
        $result = WaterModule::addWaterMeter($number, $name,$enterpriseName, $caliber, $accuracyClass, $radix, $rate, $area, $latitude, $longitude, $lastTime, $period, $fatherNode);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    public function waterGet() {
        $id = $this->request->get('id');
        $result = WaterModule::getWaterMeter($id);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        
        return view('water.water_add', $data);
    }
    //编辑水表信息
    public function waterEdit() {
        $id = $this->request->get('id');
        $name = $this->request->get('name');
        $caliber = $this->request->get('caliber');
        $accuracyClass = $this->request->get('accuracy_class');
        $radix = $this->request->get('radix');
        $rate = $this->request->get('rate') ;
        $area = $this->request->get('area');
        $latitude = $this->request->get('latitude');
        $longitude = $this->request->get('longitude');
        $fatherNode = $this->request->get('father_node');
        $enterpriseName = $this->request->get('enterprise_name');
        $result = WaterModule::editWaterMeter($id, $name, $enterpriseName, $caliber, $accuracyClass, $radix, $rate, $area, $latitude, $longitude, $fatherNode);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //编辑水表底数
    public function editWaterMeterRadix() {
        $number = $this->request->get('number');
        $name = $this->request->get('name');
        $created_at = $this->request->get('date');
        $radix = $this->request->get('radix');
        $result = WaterModule::editWaterMeterRadix($number, $name,  $radix, $created_at);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //编辑水表检测状态
    public function editWaterMeterStatus() {
        $number = $this->request->get('number');
        $name = $this->request->get('name');
        $check_time = $this->request->get('date');
        $status = $this->request->get('status');
        $result = WaterModule::editWaterMeterStatus($number, $name,$status, $check_time);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    public function downloadQrcode() {
        $id = $this->request->get('id');
        $fileName = '../storage/qrcode/' . $id . '.png';
        $url = env('WEB_URL') . "water/qrcode/".$id;
        $qrc = QrCode::format('png')->size(400)->margin(3)->generate($url);
        if (!file_exists($fileName)) {
            $myFile = fopen($fileName, 'a+');
        } else {
            $myFile = fopen($fileName, 'a+');
        }
        fwrite($myFile, $qrc);
        fclose($myFile);
        return response()->download($fileName)->deleteFileAfterSend(true);;
    }
    //获取父节点详情
    public function getFatherNode() {
        $id = $this->request->get('id');
        $result = WaterModule::getFatherNode($id);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        return view('water.water_father_node', $data);
    }
    //获取子节点想起
    public function getChildNode() {
        $id = $this->request->get('id');
        $result = WaterModule::getChildNode($id);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        return view('water.water_child_node', $data);
    }
    //获取子节点列表
    public function childList() {
        $number = $this->request->get('number');
        $result = WaterModule::childList($number);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        return $this->responseSuccessData($result['data']);
    }
    //数据报表
    public function getWaterMeterStatistic() {
        $id = $this->request->get('id');
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        if($this->request->has('length')) {
            $length = $this->request->get('length');
        } else {
            $length = 30;
        }
        $waterMeterInfoResult = WaterModule::getWaterMeter($id);
        if($waterMeterInfoResult['status'] == 'failed') {
            return view('errors.404');
        }
        $waterMeterInfo = $waterMeterInfoResult['data'];
        $waterMeterRadixListsResult = WaterModule::waterMeterDayConsumptionList($id, $page, $length);
        if($waterMeterRadixListsResult['status'] == 'success') {
            $waterMeterRadixData =  $waterMeterRadixListsResult['data'];
        } else {
            $waterMeterRadixData = [];
        }
        $data = [
            'id' => $id,
            'watermeter_info' => $waterMeterInfo,
            'watermeter_radix_data' => $waterMeterRadixData
        ];
        return view('water.water_statistic', $data);
    }
    //每周水表数据对比
    public function waterMeterWeekComparison() {
        $id = $this->request->get('id');
        $result = WaterModule::waterMeterWeekComparison($id);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }
    //每天使用量
    public function singleWaterMeterDayRadixList() {
        $id = $this->request->get('id');
        $day = $this->request->get('date');
        $waterMeterInfoResult = WaterModule::getWaterMeter($id);
        if($waterMeterInfoResult['status'] == 'failed') {
            return view('errors.404');
        }
        $waterMeterInfo = $waterMeterInfoResult['data'];
        $listResult = WaterModule::singleWaterMeterRadixListByDay($id, $day);
        if($listResult['status'] == 'failed') {
            $lists = [];
        } else {
            $lists = $listResult['data'];
        }
        $data = [
            'id' => $id,
            'watermeter_info' => $waterMeterInfo,
            'watermeter_radix_lists' => $lists
        ];
        return view('water.water_day_radix', $data);
    }
    //树状结构
    public function waterMeterTree() {
        //肿瘤医院
        $result  = WaterModule::cancerHospitalTree();
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];

        return view('water_tree', $data);
    }
   
    //首页所有总表
    public function firstNodeLevelWaterMeterList() {
        $result = WaterModule::firstNodeLevelWaterMeterList();
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        return $this->responseSuccessJsonData($result['data']);
    }
    //
    public function waterRadixExcelPage() {
        return view('water.water_excel_upload');
    }
    //excel导入水表底数
    public function importWaterMeterRadixExcel() {
        $fileName = "";
        $dir = dirname(__FILE__)."/../../../storage/app/uploads/";
        if ($this->request->isMethod('post')) {
            $file = $this->request->file('upload');
            // 文件是否上传成功
            if ($file->isValid()) {
                // 获取文件相关信息
                //$ext = pathinfo($_FILES['meinv']['name'], PATHINFO_EXTENSION);
                $baseName = pathinfo($_FILES['upload']['name'], PATHINFO_BASENAME);
                $uploadResult = Storage::disk('uploads')->put($baseName,  File::get($file));
                if(!$uploadResult) {
                    return $this->responseError("上传excel文件失败");
                }
                $fileName = $dir . "/" . $baseName;
            }
        }

        if(file_exists($fileName)) {
            $result = WaterModule::importWaterMeterRadixExcel($fileName);
        } else {
            return $this->responseError("读取excel文件失败");
        }
        return $this->responseSuccessMessage("导出excel成功");
    }
    //定时将每15分钟的数据 抽取出一天的数据， 并写入到watermeter_day_consumption
    public function autoInsertIntoWaterMeterDayConsumption() {
        $myDate = $this->request->get('date');
        $result = WaterModule::autoInsertIntoWaterMeterDayConsumption($myDate);
        return $this->responseSuccessMessage($result['message']);
    }

}